package com.crm.controller;


import com.crm.common.annotation.Log;
import com.crm.common.enums.BusinessType;
import com.crm.common.result.AjaxResult;
import com.crm.domain.SysMenu;
import com.crm.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/menu")
public class SysMenuController {


       @Autowired
       private SysMenuService sysMenuService;


    /**
     * 获取菜单列表
     */
      @GetMapping("/list")
      @Log(title = "菜单管理", businessType = BusinessType.EXPORT)
       public AjaxResult list(SysMenu menu){
          List<SysMenu> menus = sysMenuService.selectMenuList(menu);
          return AjaxResult.success(menus);
       }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping("/{menuId}")
    @Log(title = "菜单管理", businessType = BusinessType.EXPORT)
    public  AjaxResult getInfo(@PathVariable Long menuId){
        return AjaxResult.success(sysMenuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    @Log(title = "菜单管理", businessType = BusinessType.EXPORT)
    public  AjaxResult treeselect(SysMenu menu){
        List<SysMenu> menus = sysMenuService.selectMenuList(menu);
        return AjaxResult.success(menus);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping("/roleMenuTreeselect/{roleId}")
    @Log(title = "菜单管理", businessType = BusinessType.EXPORT)
    public  AjaxResult roleMenuTreeselect(@PathVariable Long roleId){
        AjaxResult ajax  = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", sysMenuService.selectMenuList(new SysMenu()));
        return ajax;
    }

    /**
     * 新增保存菜单信息
     *
     * @param menu 菜单信息
     * @return 结果
     */
    @PostMapping
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    public  AjaxResult add( @RequestBody SysMenu menu){
    menu.setMenuId(menu.getMenuId());
    return AjaxResult.success(sysMenuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PutMapping
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody SysMenu menu){
        menu.setUpdateBy("admin");
        return AjaxResult.success(sysMenuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public  AjaxResult remove(@PathVariable Long menuId){
        return AjaxResult.success(sysMenuService.deleteMenuById(menuId));
    }
}
